// MaterialPurchaseApprovalDao.java
package org.example.managementsystem.repository;

import org.example.managementsystem.model.MaterialPurchaseApproval;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class MaterialPurchaseApprovalDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public MaterialPurchaseApprovalDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void save(MaterialPurchaseApproval approval) {
        String sql = "INSERT INTO material_purchase_approval (purchase_id, is_approved, description) " +
                "VALUES (?, ?, ?)";
        jdbcTemplate.update(sql,
                approval.getPurchaseId(),
                approval.getIsApproved(),
                approval.getDescription());
    }

    public MaterialPurchaseApproval findByPurchaseId(Integer purchaseId) {
        String sql = "SELECT * FROM material_purchase_approval WHERE purchase_id = ?";
        return jdbcTemplate.queryForObject(sql, (rs, rowNum) -> {
            MaterialPurchaseApproval app = new MaterialPurchaseApproval();
            app.setPurchaseId(rs.getInt("purchase_id"));
            app.setIsApproved(rs.getBoolean("is_approved"));
            app.setDescription(rs.getString("description"));
            return app;
        }, purchaseId);
    }
}